//#include rfm

(function() {

var _isOrientationEventSupported = false,
	_isOrientationSupported = false,
	_isPortraitOverride = false,
	_meta = {},
	UNDEFINED;

(function() {
	document.body.setAttribute('onorientationchange', 'return');
	_isOrientationEventSupported
		= _isOrientationSupported
		= document.body.onorientationchange instanceof Function;
	document.body.onorientationchange = null;
	if (navigator.userAgent.indexOf('Android') > -1) {
		_isOrientationSupported = false;
	    _isPortraitOverride = screen.width < screen.height;
	    window.addEventListener('orientationchange', function(e) {
	        _isPortraitOverride = !_isPortraitOverride;
	    }, false);
	}
	var viewportMeta = document.querySelector('meta[name="viewport"]'),
		content,
		i,
		j,
		kvp,
		key,
		value;
	if (viewportMeta) {
		content = viewportMeta.getAttribute('content');
		if (content) {
			content = content.split(',');
			for (i = 0, j = content.length; i < j; i++) {
				kvp = content[i].replace(/(^\s+)|(\s+$)/g, '').split('=');
				key = kvp[0];
				value = kvp[1];
				if (value === 'no') {
					value = false;
				}
				else if (value === 'yes') {
					value = true;
				}
				else if (!isNaN(value)) {
					value = parseFloat(value);
				}
				_meta[key] = value;
			}
		}
	}
})();

rfm.Viewport = {
	/**
	 * Gets the value of a viewport meta property
	 * @param {String} property The name of the meta property
	 * @returns {Object} The value of that property
	 */
	getProperty: function(property) {
		return _meta[property];
	},
	/**
	 * Updates the viewport meta properties
	 * @param {Object<string,Object>} Hash of viewport properties to update (any combination of "width", "initial-scale", "minimum-scale", "maximum-scale" and "user-scalable")
	 */
	setProperty: function(options) {
		var viewportMeta = document.querySelector('meta[name="viewport"]'),
			content,
			key;
		if (viewportMeta) {
			content = [];
			for (key in options) {
				_meta[key] = options[key];
			}
			for (key in _meta) {
				if (content.length) {
					content.push(', ');
				}
				content.push(key, '=', typeof _meta[key] == 'boolean' ? (_meta[key] ? 'yes' : 'no') : _meta[key]);
			}
			viewportMeta.setAttribute('content', content.join(''));
		}
	},
    /**
     * Returns device orientation
     * @returns {Number} The device orientation
     */
    getOrientation: function() {
    	if (!_isOrientationSupported) {
    		return _isPortraitOverride ? 0 : 90;
    	}
    	else if (window.orientation === undefined) {
        	if (window.innerWidth < window.innerHeight) {
        		return 90;
        	}
        	else {
        		return 0;
        	}
        }
        else {
            return window.orientation;
        }
    },
    /**
     * Determines whether or not the device is in portrait mode
     * @returns {Boolean} True when the device is in portrait mode
     */
    isPortrait: function() {
        var orientation = rfm.Viewport.getOrientation();
        return orientation === 0 || orientation === 180;
    },
    /**
     * Determines whether or not the device is in landscape mode
     * @returns {Boolean} True when the device is in landscape mode
     */
    isLandscape: function() {
        var orientation = rfm.Viewport.getOrientation();
        return orientation === 90 || orientation === -90;
    },
    /**
     * Determines whether or not orientation events are supported
     * @returns {Boolean} True when orientation events are supported, false otherwise
     */
    isOrientationSupported: function() {
    	return _isOrientationEventSupported;
    },
    /**
     * Binds a callback to the orientationchange event (when supported) or the resize event (when not supported)
     * @param {Function} callback The event handler
     * @param {Boolean} capture (Optional) Whether or not to use the capturing phase (defaults to false)
     */
    bind_orientationchange: function(callback, capture) {
    	window.addEventListener(_isOrientationEventSupported ? 'orientationchange' : 'resize', callback, capture === true);
    },
    /**
     * Unbinds a callback from the orientationchange event (when supported) or the resize event (when not supported)
     * @param {Function} callback The event handler
     * @param {Boolean} capture (Optional) Whether or not to use the capturing phase (defaults to false)
     */
    unbind_orientationchange: function(callback, capture) {
    	window.removeEventListener(_isOrientationEventSupported ? 'orientationchange' : 'resize', callback, capture === true);
    }
};

})();